﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Diplom.NumericArrays;

namespace Diplom.Forms
{
    public partial class ArrayLoadFromFile : Form
    {
        Numeric1DArray numeric1DArray;

        public ArrayLoadFromFile()
        {
            InitializeComponent();

            pictureBox.Image = new System.Drawing.Bitmap(pictureBox.Width,
                pictureBox.Height);
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void buttonBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.InitialDirectory = System.IO.Directory.GetCurrentDirectory();
            openFileDialog.Filter = "Text files (*.txt,*.dat*,*.in,*.out)|*.txt;*.dat;*.in;*.out";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                if (string.IsNullOrEmpty(openFileDialog.FileName)) { return; }

                txtBoxPath.Text = openFileDialog.FileName;
                buttonLoad_Click(sender, e);
            }
        }

        private void buttonClear_Click(object sender, EventArgs e)
        {
            numeric1DArray = null;
            GC.Collect();
            
            this.Refresh();
        }

        private void ArrayLoadFromFile_Paint(object sender, PaintEventArgs e)
        {
            System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(pictureBox.Image);
            graphics.Clear(Color.White);

            if (numeric1DArray != null)
            {
                Diplom.Graphics.D2D.D2DVisualizer.DrawGraphic(ref graphics, ref numeric1DArray,
                    new RectangleF(0, 0, pictureBox.Width, pictureBox.Height), 
                    new Diplom.Graphics.D2D.DrawStyle(Color.Blue, new Point(0, 0), new Point(1, 1), Color.Red, Color.Red));
                Diplom.Graphics.D2D.D2DVisualizer.DrawSignature(ref graphics, ref numeric1DArray,
                    new RectangleF(0, 0, pictureBox.Width, pictureBox.Height),
                    new Diplom.Graphics.D2D.DrawStyle(Color.Blue, new Point(0, 0), new Point(1, 1), Color.Red, Color.Red),
                    new Size(20, 10));
            }
        }

        private void buttonLoad_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtBoxPath.Text))
            {
                MessageBox.Show("Path is empty", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtBoxPath.Focus();
                return;
            }
            if (!System.IO.File.Exists(txtBoxPath.Text))
            {
                MessageBox.Show("File not found", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtBoxPath.Focus();
                return;
            }
            numeric1DArray = new Numeric1DArray();
            Diplom.Utilities.ArrayGenerator.Generate(txtBoxPath.Text, ref numeric1DArray);
            
            this.Refresh();
        }

        public Numeric1DArray GetArray()
        {
            return numeric1DArray;
        }
    }
}
